import streamlit as st
import pandas as pd
import time

st.set_page_config(
    page_title="2.1NR",
    page_icon="✔",
    layout="wide",
    initial_sidebar_state="auto"
)

st.title(':violet[开通计划]')

df = pd.DataFrame([
    {'人员名称':' ','站点名称':' ','设备数量':0},
    {'人员名称':' ','站点名称':' ','设备数量':0},
    {'人员名称':' ','站点名称':' ','设备数量':0},
    {'人员名称':' ','站点名称':' ','设备数量':0}
])

df1 = pd.read_csv('./output/中通计划.csv')
df2 = pd.read_csv('./output/市工程计划.csv')
df3 = pd.read_csv('./output/中通建计划.csv')
df4 = pd.read_csv('./output/长讯计划.csv')

if df1.shape[0] == 0:
        df1 = df
if df2.shape[0] == 0:
        df2 = df
if df3.shape[0] == 0:
        df3 = df
if df4.shape[0] == 0:
        df4 = df

st.metric(
    label='总计划开通数',
    value=f"{df1['设备数量'].dropna(axis=0).astype(int).sum() + df2['设备数量'].dropna(axis=0).astype(int).sum() + df3['设备数量'].dropna(axis=0).astype(int).sum() + df4['设备数量'].dropna(axis=0).astype(int).sum()}台"
)

def progress1():
    with st.spinner('提交中...'):
        time.sleep(0.5)
        data1.to_csv('./output/中通计划.csv', index=False, encoding='utf-8-sig')
    st.success('提交完成！')

def progress2():
    with st.spinner('提交中...'):
        time.sleep(0.5)
        data2.to_csv('./output/市工程计划.csv', index=False, encoding='utf-8-sig')
    st.success('提交完成！')

def progress3():
    with st.spinner('提交中...'):
        time.sleep(0.5)
        data3.to_csv('./output/中通建计划.csv', index=False, encoding='utf-8-sig')
    st.success('提交完成！')

def progress4():
    with st.spinner('提交中...'):
        time.sleep(0.5)
        data4.to_csv('./output/长讯计划.csv', index=False, encoding='utf-8-sig')
    st.success('提交完成！')

col1, col2 = st.columns(2)
with col1:
    with st.form('中通'):
        st.header('中通')
        data1 = st.data_editor(df1, hide_index=True, use_container_width=True, num_rows='dynamic')
        submit1 = st.form_submit_button(label='提交')
        if submit1:
            progress1()
            st.experimental_rerun()

with col2:
    with st.form('市工程'):
        st.header('市工程')
        data2 = st.data_editor(df2, hide_index=True, use_container_width=True, num_rows='dynamic')
        submit2 = st.form_submit_button(label='提交')
        if submit2:
            progress2()
            st.experimental_rerun()

col3, col4 = st.columns(2)
with col3:
    with st.form('中通建'):
        st.header('中通建')
        data3 = st.data_editor(df3, hide_index=True, use_container_width=True, num_rows='dynamic')
        submit3 = st.form_submit_button(label='提交')
        if submit3:
            progress3()
            st.experimental_rerun()
with col4:
    with st.form('长讯'):
        st.header('长讯')
        data4 = st.data_editor(df4, hide_index=True, use_container_width=True, num_rows='dynamic')
        submit4 = st.form_submit_button(label='提交')
        if submit4:
            progress4()
            st.experimental_rerun()